var tp_arr = new Array(
    "/static/app/transverse/system/auth03/agent/index.htm"
);
var keywordStr = window.getUrlParam("k");
var pageNo = window.getUrlParam("p");
var totle = window.getUrlParam("t");
var ri = window.getUrlParam("ri");
console.log(window.location);
window.current = new Vue({
    el: '#current',
    template: window.getTemplate(tp_arr),
    data: {
        pages: [],
        totle: totle ? totle : 0,
        page: pageNo ? pageNo : 1,
        size: 10,
        keyword: keywordStr ? keywordStr : '',
        agentList: [],
        agentSumFind: '',
        regionID: ri ? ri : ''
    },
    // 属性监听器
    watch: {},
    filters: {
        _formatMoney: function (val) {
            var money = parseFloat(val);
            return window.formatMoney(money, 0);
        },
        _getRowsColor: function (index) {
            var className = 'even';
            if(index % 2 == 0){
                className = '';
            }
            return className;
        },
        _getTargetColor: function (index, type) {
            //console.log(index);
            //console.log(type);
            var info = window.current['agentList'][index];
            var className = '';
            if((parseFloat(info['target' + type + '_value']) > 0) && (parseFloat(info['target' + type + '_sum']) / parseFloat(info['target' + type + '_value'])) > 1){
                className = ['green-bg','color-white'];
            }
            return className;
        },
        _getTargetProportion: function (type) {
            var styleVal = 'width: 0%;';
            var proportion = 0;
            var info = window.current['agentSumFind'];
            if(info != ''){
                if(type == 0){
                    proportion = (parseFloat(info['target1_sum']) + parseFloat(info['target2_sum']) + parseFloat(info['target3_sum'])) / (parseFloat(info['target1_value']) + parseFloat(info['target2_value']) + parseFloat(info['target3_value'])) * 100;
                } else {
                    proportion = parseFloat(info['target' + type + '_sum']) / parseFloat(info['target' + type + '_value']) * 100;
                }
            }
            proportion = parseFloat(proportion.toFixed(2));
            if(proportion > 0){
                styleVal = 'width: ' + proportion +'%;';
            }
            return styleVal;
        },
        _totalTargetFormatMoney: function (type) {
            var money = 0;
            var info = window.current['agentSumFind'];
            if(info != ''){
                if(type == 1){
                    money = parseFloat(info['target1_sum']) + parseFloat(info['target2_sum']) + parseFloat(info['target3_sum']);
                }
                if(type == 2){
                    money = parseFloat(info['target1_value']) + parseFloat(info['target2_value']) + parseFloat(info['target3_value']);
                }
            }
            return window.formatMoney(money, 0);
        },
        _getProportion: function (type) {
            var proportion = 0;
            var info = window.current['agentSumFind'];
            if(info != ''){
                if(type == 0){
                    proportion = (parseFloat(info['target1_sum']) + parseFloat(info['target2_sum']) + parseFloat(info['target3_sum'])) / (parseFloat(info['target1_value']) + parseFloat(info['target2_value']) + parseFloat(info['target3_value'])) * 100;
                } else {
                    proportion = parseFloat(info['target' + type + '_sum']) / parseFloat(info['target' + type + '_value']) * 100;
                }
            }
            return proportion?proportion.toFixed(2) : 0;
        },
        _currPage: function (val) {
            var page = window.current['page'];
            var className = 'footable-page';
            if(page == val){
                className = ['footable-page', 'active']
            }
            return className;
        }
    },
    computed: {},
    methods: {
        onSearch: function () {
            var keyword = this['keyword'];
            var region_id = this['regionID'];
            var ps = this['personState'];
            var hrefStr =  window.location.pathname + '?ps=' + ps;
            if(keyword != ''){
                hrefStr += '&k=' + keyword;
            }
            if(region_id != ''){
                hrefStr += '&ri=' + region_id;
            }
            window.location.href = hrefStr;
        },
        onSetPage: function (val) {
            var page = this['page'];
            var totle = this['totle'];
            var keyword = this['keyword'];
            var ps = this['personState'];
            if(page != val){
                var hrefStr =  window.location.pathname + '?ps=' + ps;
                if((keywordStr == null && keyword == '')|| keywordStr == keyword){
                    hrefStr += '&p=' + val + '&t=' + totle;
                }
                if(keyword != ''){
                    hrefStr += '&k=' + keyword;
                }
                window.location.href = hrefStr;
            }
        },
        onToAgentDetail: function (index) {
            var info = this['agentList'][index];
            var rf_url = window.location.pathname + window.location.search;
            var url = "/system/Auth03/detail.html?c=" + info['agency_code'] + "&rf=" + encodeURIComponent(rf_url);

            window.location.href = url;
        }
    }
});

window.get_agent_list = function () {
    var page = window.current['page'];
    var size = window.current['size'];
    var keyword = window.current['keyword'];
    var region_id = window.current['regionID'];
    var data_arr = {
        page: page,
        size: size,
        keyword : keyword,
        region_id : region_id
    };
    window.ajax({
        url: "/service/Partypromotion/getAgentList",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                var list = d['data'];
                if (list) {
                    var size = window.current['size'];
                    var totle = window.current['totle'];
                    window.current['agentList'] = list;
                    if (totle == 0) {
                        window.get_agent_list_count();
                    }else {
                        window.setPages(totle);
                    }
                } else {
                    window.current['agentList'] = [];
                    window.current['totle'] = 0;
                }
                window.get_agent_sum();
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.get_agent_sum = function () {
    var keyword = window.current['keyword'];
    var region_id = window.current['regionID'];
    var data_arr = {
        keyword : keyword,
        region_id : region_id
    };
    window.ajax({
        url: "/service/Partypromotion/getAgentSum",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                var data = d['data'];
                if (data) {
                    window.current['agentSumFind'] = data;
                } else {
                    window.current['agentSumFind'] = '';
                }
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.get_agent_list_count = function () {
    var keyword = window.current['keyword'];
    var region_id = window.current['regionID'];
    var data_arr = {
        keyword : keyword,
        region_id : region_id
    };
    window.ajax({
        url: "/service/Partypromotion/getAgentListCount",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                var totle = parseInt(d['data']);
                if (totle > 0) {
                    window.setPages(totle);
                }
                window.current['totle'] = totle;
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.setPages = function (count) {
    var size = window.current['size'];
    var pageCount = Math.ceil(count / size);
    var pages = [];
    for(let i = 1; i　<= pageCount; i++) {
        pages.push(i);
    }
    window.current['pages'] = pages;
}

window.get_agent_list();
